﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;

namespace DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects
{
    public class ControlPanel : WatiNBase
    {
        #region Constructors

        public ControlPanel(WatiNBase watinBase) : base(watinBase.IEInstance, watinBase.SiteUrl, watinBase.DBName) { }

       
        #endregion

        #region Public Properties

        #region Images
        public Image MaximiseControlPanelImage { get { return ControlPanelTable.Image(Find.ByTitle("Maximize")); } }
        public Image MinimiseControlPanelImage { get { return ControlPanelTable.Image(Find.ByTitle("Minimize")); } }
        #endregion

        #region Links
        public Link AddModuleLink
        {
            get { return IEInstance.Link(Find.ById(s => s.EndsWith("IconBar.ascx_cmdAddModule"))); }
        }
        public Link AddPageLink
        {
            get { return ControlPanel.Link(Find.ById(s => s.EndsWith("IconBar.ascx_cmdAddTab"))); }
        }
        public Link AddPageLinkAlt
        {
            get { return ControlPanelAlt.Link(Find.ById(s => s.EndsWith("IconBar.ascx_cmdAddTab"))); }
        }
        public Link ImportPageLink
        {
            get { return IEInstance.Link(Find.ById(s => s.EndsWith("IconBar.ascx_cmdImportTab"))); }
        }
        public Link PageSettingsLink
        {
            get { return IEInstance.Link(Find.ById(s => s.EndsWith("IconBar.ascx_cmdEditTab"))); }
        }
        public Link CopyPageLink
        {
            get { return IEInstance.Link(Find.ById(s => s.EndsWith("IconBar.ascx_cmdCopyTab"))); }
        }
        public Link DeletePageLink
        {
            get { return IEInstance.Link(Find.ById(s => s.EndsWith("IconBar.ascx_cmdDeleteTab"))); }
        }
        public Link ExportPageLink
        {
            get { return IEInstance.Link(Find.ById(s => s.EndsWith("IconBar.ascx_cmdExportTab"))); }
        }
        public Link AdminLink
        {
            get { return IEInstance.Link(Find.ById(s => s.EndsWith("IconBar.ascx_cmdAdmin"))); }
        }
        public Link HostLink
        {
            get { return IEInstance.Link(Find.ById(s => s.EndsWith("IconBar.ascx_cmdHost"))); }
        }
        
        public Link ExtensionsLink
        {
            get { return IEInstance.Link(Find.ById(s => s.EndsWith("IconBar.ascx_cmdExtensions"))); }
        }
        public Link FilesLink
        {
            get { return IEInstance.Link(Find.ById(s => s.EndsWith("IconBar.ascx_cmdFiles"))); }
        }
        public Link HelpLink
        {
            get { return IEInstance.Link(Find.ById(s => s.EndsWith("IconBar.ascx_cmdHelp"))); }
        }
        public Link RolesLink
        {
            get { return IEInstance.Link(Find.ById(s => s.EndsWith("IconBar.ascx_cmdRoles"))); }
        }
        public Link SiteSettingsLink
        {
            get { return IEInstance.Link(Find.ById(s => s.EndsWith("IconBar.ascx_cmdSite"))); }
        }
        public Link UsersLink
        {
            get { return IEInstance.Link(Find.ById(s => s.EndsWith("IconBar.ascx_cmdUsers"))); }
        }
        #endregion

        #region SelectLists
        public SelectList ExistingModulePageSelectList
        {
            get { return IEInstance.SelectList(Find.ById("dnn_IconBar.ascx_cboTabs")); }
        }
        public SelectList ExistingModuleSelectList
        {
            get { return IEInstance.SelectList(Find.ById("dnn_IconBar.ascx_cboModules")); }
        }
        public SelectList InsertRelativeSelectList
        {
            get { return IEInstance.SelectList(Find.ById("dnn_IconBar.ascx_cboInstances")); }
        }
        public SelectList ModuleInsertSelect
        {
            get { return IEInstance.SelectList(Find.ById("dnn_IconBar.ascx_cboPosition")); }
        }
        public SelectList ModulePaneSelect
        {
            get { return IEInstance.SelectList(Find.ById("dnn_IconBar.ascx_cboPanes")); }
        }
        public SelectList ModuleSelect
        {
            get { return IEInstance.SelectList(Find.ById("dnn_IconBar.ascx_cboDesktopModules")); }
        }
        public SelectList ModuleVisibilitySelect
        {
            get { return IEInstance.SelectList(Find.ById("dnn_IconBar.ascx_cboPermission")); }
        }
        #endregion

        #region TextFields 
        public TextField ModuleTitleField
        {
            get { return IEInstance.TextField(Find.ById("dnn_IconBar.ascx_txtTitle")); }
        }
        #endregion

        #region RadioButtons
        public RadioButton AddExistingModuleRadioButton
        {
            get { return IEInstance.RadioButton(Find.ById("dnn_IconBar.ascx_optModuleType_1")); }
        }
        public RadioButton EditRadioButton { get { return ControlPanelTable.RadioButton(Find.ByValue("EDIT")); } }
        public RadioButton ViewRadioButton { get { return ControlPanelTable.RadioButton(Find.ByValue("VIEW")); } }
        public RadioButton LayoutRadioButton { get { return ControlPanelTable.RadioButton(Find.ByValue("LAYOUT")); } }
        #endregion

        #region Table
        public Table ControlPanelTable
        {
            get { return IEInstance.Table(Find.ById(s => s.EndsWith("IconBar.ascx_tblControlPanel"))); }
        }
        #endregion


        #endregion

        #region Public Methods
        public void AddExistingModuleToPage(string pageName, string moduleName)
        {
            AddExistingModuleRadioButton.Checked = true;
            System.Threading.Thread.Sleep(3000);
            ExistingModulePageSelectList.Select(pageName);
            System.Threading.Thread.Sleep(1000);
            ExistingModuleSelectList.Select(moduleName);
            AddModuleLink.Click();

        }

        public void AddHTMLModuleToPageSelectVisibility(string moduleName, string moduleType, string visibilityChoice, string content, int moduleIndex)
        {
            ModuleSelect.Select(moduleType);
            if (!moduleName.Equals(""))
            {
                ModuleTitleField.Value = moduleName;
            };
            ModuleVisibilitySelect.Select(visibilityChoice);
            AddModuleLink.Click();
            System.Threading.Thread.Sleep(2000);
            ModulePage modulePage = new ModulePage(this);
            modulePage.AddContentToModule(content, moduleIndex);
        }

        public void AddHTMLModuleToPageWithContent(string moduleName, string moduleType, string content, int moduleIndex)
        {
            ModuleSelect.Select(moduleType);
            if (!moduleName.Equals(""))
            {
                ModuleTitleField.Value = moduleName;
            }
            AddModuleLink.Click();

            System.Threading.Thread.Sleep(2000);
            ModulePage modulePage = new ModulePage(this);
            modulePage.AddContentToModule(content, moduleIndex);
        }
       
        public void AddModuleToPage(string moduleName, string moduleType)
        {
            ModuleSelect.Select(moduleType);
            if (!moduleName.Equals(""))
            {
                ModuleTitleField.Value = moduleName;
            }
            AddModuleLink.ClickNoWait();
        }

        public void AddModuleToPageInsert(string moduleName, string moduleType, string insert, string otherPage)
        {
            ModuleSelect.Select(moduleName);
            if (!moduleName.Equals(""))
            {
                ModuleTitleField.Value = moduleName;
            }
            ModuleInsertSelect.Select(insert);
            if (insert.CompareTo("Above") == 0 || insert.CompareTo("Below") == 0)
            {
                InsertRelativeSelectList.Select(otherPage);
            }
            AddModuleLink.Click();

        }

        public void SelectPageDisplayMode(string pageDisplayMode)
        {
            if (!string.IsNullOrEmpty(pageDisplayMode))
                WatiNUtil.SelectRadioButtonByName(IEInstance, "dnn$IconBar.ascx$optMode", pageDisplayMode);
        }
        
        #endregion
    }
}
